﻿using BLL;
using Model;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace OAS
{
    public partial class UserInfo : System.Web.UI.Page
    {
        /// <summary>

        /// 绑定下拉框

        /// </summary>

        /// <param name="ddl">下拉框控件</param>

        /// <param name="source">数据源</param>

        /// <param name="textField">显示文本字段名称</param>

        /// <param name="valueField">隐式的值字段名称</param>

        protected void BindDLL(DropDownList ddl, IEnumerable source, string textField, string valueField)
        {
            if (source != null)
            {
                ddl.DataTextField = textField;
                ddl.DataValueField = valueField;
                ddl.DataSource = source;
                ddl.DataBind();
                ddl.Items.Insert(0, "");
            }
        }
        UserInfoBLL userInfoBLL = new UserInfoBLL();
        /// <summary>

        /// 绑定基本信息

        /// </summary>

        public void BindMessage()
        {
            BindDLL(RoleDropDownList, userInfoBLL.GetRoleList(), "Description", "Id");
            FactoryDropDownList.DataSource = userInfoBLL.GetFactoryList();
            FactoryDropDownList.DataBind();
            FactoryDropDownList.Items.Insert(0, "");
            Search();
        }


        /// <summary>

        /// 查询方法

        /// </summary>

        private void Search()
        {
            UserListViewEntity whereEntity = new UserListViewEntity();

            whereEntity.UserAccount = UserAccountTextBox.Text;
            whereEntity.UserName = UserNameTextBox.Text;
            whereEntity.Title = TitleTextBox.Text;
            if (!string.IsNullOrEmpty(RoleDropDownList.SelectedValue))
            {
                whereEntity.RoleId = Convert.ToInt32(RoleDropDownList.SelectedValue);
            }
            if (!string.IsNullOrEmpty(FactoryDropDownList.SelectedValue))
            {
                whereEntity.FactoryCode = FactoryDropDownList.SelectedValue;
            }

            //总条数

            var totalCount = 0;
            //获取分页数据

            var pageData = UserInfoBLL.GetUserInfoList(whereEntity, AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize, out totalCount);
            //获取并设置总条数

            AspNetPager1.RecordCount = totalCount;
            //设置GridView的数据源

            UserListGridView.DataSource = pageData;
            UserListGridView.DataBind();
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindMessage();
            }
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            Search();
        }

        protected void SearchButton_Click(object sender, EventArgs e)
        {
            //设置当前页为第一页

            AspNetPager1.CurrentPageIndex = 0;
            Search();
        }

        /// <summary>

        /// 添加用户

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        protected void editable_Click(object sender, EventArgs e)
        {
            Response.Redirect("/UserInfoUpdate.aspx?id=0");
        }
        /// <summary>

        /// 用户信息操作

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        protected void UserListGridView_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "Edit":
                    Response.Redirect("/UserInfoUpdate.aspx?id=" + e.CommandArgument);
                    break;

                case "Delete":
                    userInfoBLL.DeleteUserInfo(Convert.ToInt32(e.CommandArgument));
                    Response.Redirect("/UserInfo.aspx");
                    break;
            }
        }
    }
}
